package com.mohe.zhihui.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mohe.zhihui.model.Message;
import com.mohe.zhihui.model.Notice;
import com.mohe.zhihui.model.Project;
import com.mohe.zhihui.model.Volunteer;
import com.mohe.zhihui.model.Volunteerprj;
import com.mohe.zhihui.service.interfaces.IMessage;
import com.mohe.zhihui.service.interfaces.INotice;
import com.mohe.zhihui.service.interfaces.IVolunteer;
import com.mohe.zhihui.utils.Json;
import com.mohe.zhihui.utils.JsonUtil;
import com.mohe.zhihui.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

public class VolunteerAction extends ActionSupport {

	private static final long serialVersionUID = -249396030692382302L;

//	ApplicationContext ac;

	private IVolunteer volunteerServ;

	private List<Volunteer> volunteerList;
	private Volunteer volunteer = new Volunteer();
	private Page page = new Page();

	private String projects; // 一个志愿者的加入的所有项目

	public IVolunteer getVolunteerServ() {
		return volunteerServ;
	}

	public void setVolunteerServ(IVolunteer volunteerServ) {
		this.volunteerServ = volunteerServ;
	}

	public List<Volunteer> getVolunteerList() {
		return volunteerList;
	}

	public void setVolunteerList(List<Volunteer> volunteerList) {
		this.volunteerList = volunteerList;
	}

	public Volunteer getVolunteer() {
		return volunteer;
	}

	public void setVolunteer(Volunteer volunteer) {
		this.volunteer = volunteer;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getProjects() {
		return projects;
	}

	public void setProjects(String projects) {
		this.projects = projects;
	}

//	public VolunteerAction() {
//		super();
//		ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//		volunteerServ = (IVolunteer) ac.getBean("IVolunteer");
//	}

	public String getVolunteerListForPage() {
		int count = volunteerServ.getCount(volunteer);
		page.setCount(count);
		volunteerList = volunteerServ.getVolunteerList(volunteer, page);
		return "volunteerList.jsp";
	}

	public void searchVolunteerList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = volunteerServ.getCount(volunteer);
		page.setCount(count);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Volunteer> list = volunteerServ.getVolunteerList(volunteer, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (Volunteer s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getName())) {
					map.put("name", s.getName());
				} else {
					map.put("name", "");
				}
				if (!StringUtils.isBlank(s.getPhone())) {
					map.put("phone", s.getPhone());
				} else {
					map.put("phone", "");
				}
				if (s.getCreateDate() != null) {
					map.put("date", df.format(s.getCreateDate()));
				} else {
					map.put("date", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}

	/**
	 * 保存志愿者信息
	 */
	public void saveVolunteer() {
		// Json json = new Json();
		// boolean result = messageServ.saveMessage(message);
		// if (result) {
		// json.setMessage("添加成功");
		// } else {
		// json.setSuccess(false);
		// json.setMessage("添加失败");
		// }
		// JsonUtil.writeJson(json);
	}

	public String toEditVolunteer() {
		// message = messageServ.getMessageById(message.getTimeKey());
		// return "updateNotice.jsp";
		return "";
	}

	public void updateVolunteer() {
		// Json json = new Json();
		// Notice tmp = noticeServ.getNoticeById(notice.getTimeKey());
		// if (tmp != null) {
		// tmp.setTitle(notice.getTitle());
		// tmp.setContent(notice.getContent());
		// tmp.setCreateDate(new Date());
		// boolean result = noticeServ.updateNotice(tmp);
		// if (result) {
		// json.setMessage("编辑成功");
		// notice = tmp;
		// } else {
		// json.setSuccess(true);
		// json.setMessage("编辑失败");
		// }
		// } else {
		// json.setSuccess(true);
		// json.setMessage("编辑失败");
		// }
		// JsonUtil.writeJson(json);
	}

	public void delVolunteer() {
		// Json json = new Json();
		// boolean result = noticeServ.delNotice(notice);
		// if (result) {
		// json.setMessage("删除成功");
		// } else {
		// json.setSuccess(false);
		// json.setSuccess(false);
		// }
		// JsonUtil.writeJson(json);
	}

	// 显示志愿者详细信息
	public String showVolunteer() {
		volunteer = volunteerServ.getVolunteerById(volunteer.getTimeKey());
		// // 项目
		// Iterator<Volunteerprj> it = volunteer.getVolunteerprjs().iterator();
		// Volunteerprj v;
		// Project p;
		// projects = "";
		// if(it != null){
		// while(it.hasNext()){
		// v = (Volunteerprj) it.next();
		// p = v.getProject();
		// projects += p.getTitle() + ",";
		// }
		// if(!projects.isEmpty()){
		// projects = projects.substring(0, projects.length() - 1);
		// }
		// }

		// 项目
		Iterator<Project> it = volunteer.getProjects().iterator();
		Project p;
		projects = "";
		if (it != null) {
			while (it.hasNext()) {
				p = (Project) it.next();
				projects += p.getTitle() + ",";
			}
			if (!projects.isEmpty()) {
				projects = projects.substring(0, projects.length() - 1);
			}
		}

		return "showVolunteer.jsp";
	}

}